

package com.latam.arq.example.web.managedbeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.latam.arq.commons.exceptions.LATAMException;
import com.latam.arq.example.crud.ejb.services.AdmisionCRUDService;
import com.latam.arq.example.crud.ejb.services.PamCRUDService;
import com.latam.arq.example.domain.AdmisionBean;
import com.latam.arq.example.domain.PamBean;
 
@ManagedBean(name = "selectorAdmisionMB")
@RequestScoped
public class SelectorAdmisionMB implements Serializable {
     
	
	private static final Logger logger = LoggerFactory.getLogger(SelectorAdmisionMB.class);
	
    private List<ColumnModel> columns;
    private List<AdmisionBean> admisionFilterBean;

	@EJB
	private AdmisionCRUDService admisionEJBServices;
		
    private List<AdmisionBean> admision;
 
    @PostConstruct
    public void init() {
		try{
			this.admision = admisionEJBServices.list();
		
			
			for (AdmisionBean admision : this.admision) {
				
				logger.info(admision.getRolclinica());
			
			}
			
		}catch(final LATAMException e){
			this.admision = new ArrayList<AdmisionBean>();
		}
		createDynamicColumns();
    }
     
    public void selectAdmisionFromDialog(AdmisionBean admision) {
        RequestContext.getCurrentInstance().closeDialog(admision);
    }
    
    public List<AdmisionBean> getAdmision() {
		return admision;
	}



	public void setAdmision(List<AdmisionBean> admision) {
		this.admision = admision;
	}



	public List<AdmisionBean> getAdmisionFilterBean() {
		return admisionFilterBean;
	}

	public void setAdmisionFilterBean(List<AdmisionBean> admisionFilterBean) {
		this.admisionFilterBean = admisionFilterBean;
	}


 
    public List<ColumnModel> getColumns() {
        return columns;
    }
 
    private void createDynamicColumns() {

    	
        columns = new ArrayList<ColumnModel>();  
        columns.add(new ColumnModel("Rol Clinica","rolclinica"));
        columns.add(new ColumnModel("Rut Paciente","pacienterutpaciente"));
        columns.add(new ColumnModel("Rut Profesional","profesionalrutprofesional"));
        
         
    }
     
    public void updateColumns() {
        //reset table state
        UIComponent table = FacesContext.getCurrentInstance().getViewRoot().findComponent(":form:admisions");
        table.setValueExpression("sortBy", null);
         
        //update columns
        createDynamicColumns();
    }
     
    static public class ColumnModel implements Serializable {
 
        private String header;
        private String property;
 
        public ColumnModel(String header, String property) {
            this.header = header;
            this.property = property;
        }
 
        public String getHeader() {
            return header;
        }
 
        public String getProperty() {
            return property;
        }
    }
    
    
    
    
}
